<template>
  <div id="buMenList">
    <el-card class="box-card"  style="height: 100%;">
      <div slot="header" class="clearfix" style="text-align: right">
        <el-button size="medium" type="goon" round @click="dialogVisible = true" style="" v-has="'addunit'">创建部门</el-button>
      </div>
      <div class="serverTable">
      <el-table
        :data="tableData"
        border
        height="100%"
        style="width: 100%">
        <el-table-column
          type="selection"
        >
        </el-table-column>
        <el-table-column
          type="index"
          label="#"
          min-width="2%"
        >
          <template v-slot="scope">
            {{ (queryInfo.pageNumber-1) * queryInfo.pageSize+ scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column
          prop="unitName"
          label="部门名称"
          min-width="20%"
        >
        </el-table-column>
        <el-table-column
          prop="unitAddress"
          label="部门地址"
          min-width="45%"
        >
        </el-table-column>
        <el-table-column
          label="所属师级"
          min-width="25%"
        >
          <template  v-slot="scope">
              {{scope.row.cityName}}{{scope.row.countyName}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="13%"
        >
          <template v-slot="scope">
            <el-tooltip effect="dark" content="修改部门" placement="top" :enterable="false" v-has="'updateunit'">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDialog(scope.row.id)" ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除部门" placement="top" :enterable="false" v-has="'deleteunit'">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteMsg(scope.row.id)" ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNumber"
        :page-sizes="[5,10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      </div>
    </el-card>
    <el-dialog
      title="添加部门"
      :visible.sync="dialogVisible"
      width="25%"
      center
      :before-close="handleClose"
      @close="dialogClose"
    >
      <el-form ref="addFormRef" :model="addForm" label-width="80px" :rules="addFormRule">
        <el-form-item label="部门名称" prop="unitName">
          <el-input size="medium" v-model="addForm.unitName"></el-input>
        </el-form-item>
        <el-form-item label="部门地址" prop="unitAddress">
          <el-input size="medium" v-model="addForm.unitAddress"></el-input>
        </el-form-item>
        <el-form-item label="所属师级" prop="addCity">
      <!--  <el-cascader
          :options="shiTuanData"
          ref="shiCascader"
          :props="optionProps"
          style="width: 100%;"
          @change="addShituan"
          ></el-cascader>-->
          <el-select v-model="addForm.addCity" clearable   placeholder="请选择所属师级" style="width: 100%" @change="cityChange">
            <el-option
              v-for="item in addForm.shiTuanData"
              :key="item.id"
              :label="item.name"
              :value="{value:item.id,label:item.name,code:item.code}">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button size="medium" type="goon" round @click="dialogVisible = false">取 消</el-button>
    <el-button size="medium" type="goon" round @click="addBumen" >确 定</el-button>
  </span>
    </el-dialog>
    <el-dialog
      title="修改部门信息"
      :visible.sync="editdialogVisible"
      width="25%"
      center
      :before-close="handleClose"
      @close="editdialogClose"
    >
      <el-form ref="editFormRef" :model="editForm" label-width="80px" :rules="editFormRules">
        <el-form-item label="部门名称" prop="unitName">
          <el-input size="medium" v-model="editForm.unitName"></el-input>
        </el-form-item>
        <el-form-item label="部门地址" prop="unitAddress">
          <el-input size="medium" v-model="editForm.unitAddress"></el-input>
        </el-form-item>
        <el-form-item label="所属师级" prop="cityName">
        <!--  <el-cascader
            :options="shiTuanData"
            v-model="city"
            ref="shiCascader2"
            :props="optionProps"
            @change="changeShituan"
            style="width: 100%"
            ></el-cascader>-->
          <el-select v-model="editForm.cityName" clearable   placeholder="请选择所属师级" style="width: 100%" @change="cityChange2">
            <el-option
              v-for="item in addForm.shiTuanData"
              :key="item.id"
              :label="item.name"
              :value="{value:item.id,label:item.name,code:item.code}">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="goon" size="medium" round @click="editdialogVisible = false">取 消</el-button>
    <el-button type="goon" size="medium" round @click="editBumenInfo">确 定</el-button>
  </span>
    </el-dialog>
  </div>

</template>

<script>
  export default {
    name: 'buMenList',
    data(){
      return{
        value:[],
        city:null,
        queryInfo:{
          unitName:'',
          pageNumber:1,
          pageSize:10,
        },
        total:10,
        tableData:[],
        dialogVisible:false,
        addForm:{
          unitName:'',
          unitAddress:'',
          cityCode:'',
          cityId:0,
          cityName:'',
          countyId:0,
          countyName:'',
          isCity:0,
          shiTuanData:[],
          addCity:''
        },
        addFormRule:{
          unitName:[{required:true,message:'请输入部门名',trigger:'blur'}],
          addCity:[{required:true,message:'请选择所属师',trigger:'blur'}],
        },
        editdialogVisible:false,
        editForm:{
        },
        editFormRules:{
          unitName:[{required:true,message:'请输入部门名',trigger:'blur'}],
          cityName:[{required:true,message:'请选择所属师',trigger:'blur'}],
        },
        optionProps:{
          checkStrictly: true,
          value: 'id',
          label: 'name',
          children: 'children'
        },
      }
    },
    created () {
      this.getBumenList();
      this.getShituan();
    },
    methods:{
      async getBumenList(){
        const {data:res}=await  this.$http3.get('/v1.0/unit/findPages',{
          params:this.queryInfo
        })
        if(res.status!=="3000"){
           this.$message.error('获取部门列表失败！' +res.message);
          return this.tableData=[];
        }
        this.tableData=res.data.result;
        this.total=res.data.totalCount
      },
      async addBumen(){
        this.$refs.addFormRef.validate( async valid =>{
          if(!valid)
            return
          const {data:res}= await this.$http3.post('/v1.0/unit/save', this.addForm)
          if(res.status!=="6000"){
            return  this.$message.error('添加部门失败！')
          }
          this.$message.success('添加部门成功！');
          this.getBumenList();
          this.dialogVisible = false;
        })
      },
      dialogClose(){
        this.$refs.addFormRef.resetFields();
      },
      cityChange(e){
        console.log(e)
        const { value, label ,code} = e;
        this.addForm.cityId=value;
        this.addForm.addCity=this.addForm.cityName=label;
        this.addForm.cityCode=code
        console.log(this.addForm)
      },
      cityChange2(e){
        console.log(
          e
        )
        const { value, label,code } = e;
        this.editForm.cityId=value;
        this.editForm.addCity=this.editForm.cityName=label;
        this.editForm.cityCode=code
        console.log(this.editForm)
      },
      async editDialog(id){
        //请求查询
        const {data:res}=await  this.$http3.get('/v1.0/unit/findById/'+id);
        if(res.status!=="3000"){
          return 0;
        }

        this.editForm=res.data;
        this.city=this.editForm.cityId;
        this.editdialogVisible = true
      },
      editdialogClose(){
        this.$refs.editFormRef.resetFields()
      },
      editBumenInfo(){
        this.$refs.editFormRef.validate(async valid =>{
          if(!valid) return
          const {data:res}= await this.$http3.post('/v1.0/unit/update', this.editForm)
          if(res.status!=="6000"){
            return  this.$message.error('修改部门失败！')
          }
          this.$message.success('修改部门成功！');
          this.editdialogVisible = false;
          this.getBumenList();
        })
      },
      async deleteMsg(id){
        const confirmResult = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch((err) => {
          return err
        });
        if(confirmResult !== 'confirm'){
          return this.$message({
            type: 'info',
            message: '已取消删除'
          });
        }
        //请求
        const {data:res}= await this.$http3.delete('/v1.0/unit/delete/'+id)
        if(res.status!="4000"){
          return  this.$message.error('删除部门失败！')
        }
        this.$message.success('删除部门成功！');
        this.getBumenList();
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      async getShituan(){
        //const {data:res}=await  this.$http3.get('/v1.0/unit/find/city/findAllCityAndCounty')
        const {data:res}=await  this.$http3.get('/v1.0/unit/find/city/findAllByDivision')
        if(res.status!=="3000"){
          return
        }
        for (let i=0;i<res.data.length;i++){
          if(res.data[i].isShow==1){
            if(res.data[i].code==65){
              this.addForm.shiTuanData.unshift({id:res.data[i].id,name:res.data[i].name,code:res.data[i].code})
            }else {
              this.addForm.shiTuanData.push({id:res.data[i].id,name:res.data[i].name,code:res.data[i].code})
            }
          }
        }
      },
      addShituan(code){
        if(code.length==1){
          this.addForm.isCity=1;
          this.addForm.cityId=code[0];
          this.addForm.cityName=this.$refs['shiCascader'].getCheckedNodes()[0].label;

        }else if(code.length==2){
          this.addForm.isCity=0;
          this.addForm.cityId=code[0];
          this.addForm.countyId=code[1];
          this.addForm.cityName=this.$refs['shiCascader'].getCheckedNodes()[0].parent.label;
          this.addForm.countyName=this.$refs['shiCascader'].getCheckedNodes()[0].label;
        }

      },
      changeShituan(code){
        if(code.length==1){
          this.editForm.isCity=1;
          this.editForm.cityId=code[0];
          this.editForm.cityName=this.$refs['shiCascader2'].getCheckedNodes()[0].label;
        }else if(code.length==2){
          this.editForm.isCity=0;
          this.editForm.cityId=code[0];
          this.editForm.countyId=code[1];
          this.editForm.cityName=this.$refs['shiCascader2'].getCheckedNodes()[0].parent.label;
          this.editForm.countyName=this.$refs['shiCascader2'].getCheckedNodes()[0].label;
        }
        console.log(this.editForm)
      },
      handleSizeChange(newSize){
        this.queryInfo.pageSize=newSize;
        this.getBumenList()
      },
      handleCurrentChange(newPage){
        this.queryInfo.pageNumber=newPage;
        this.getBumenList()
      },
    }
  }
</script>

<style scoped lang="less">
  #buMenList{
    position: absolute;
    left: 15.5%;
    top: 9.5%;
    bottom: 1.5%;
    width: 83.5%;
    /*background-color: #ffffff;*/
    border-radius: 8px;
    box-shadow: 0 0 5px #acc4fe;
  }
  ::v-deep .cell{
    text-align: center;
  }
  /deep/ .el-table th.gutter{
    display: table-cell!important;
  }
  .el-row{
    ::v-deep  .el-input__inner{
      border-radius: 45px;
    }

  }
  .clearfix{
    ::v-deep .el-button:focus, .el-button:hover {
      color: #fff;
      border-color: #c6e2ff;
      background: -webkit-linear-gradient(left,#3ee8c2,#25e4a3);
    }
    ::v-deep .el-button--goon:active {
      color: #fff;
      border-color: #c6e2ff;
      outline: 0;
    }
  }

  /deep/ .el-input-group__append{
    border-radius: 45px;
  }
  /deep/ .el-dialog{
    border-radius: 8px;
  }
  /deep/ .el-dialog__header {
    //background: -webkit-linear-gradient(left, #3ee8c2, #25e4a3);

    border-radius: 8px 8px 0 0 ;
  }

  ::v-deep .el-button--goon:focus, .el-button--goon:hover {
    color: #fff;
    border-color: #c6e2ff;
    background: -webkit-linear-gradient(left,#3ee8c2,#25e4a3);
  }
  ::v-deep .el-button--goon:active {
    color: #fff;
    border-color: #c6e2ff;
    outline: 0;
  }
  ::v-deep .el-dialog__title{
    font-weight: bold;
    font-size: 20px;
    color: #45524f;
  }
  .el-dialog__body{
    .el-form{
      .el-form-item {
        .el-form-item__content{
          .el-input{
            ::v-deep  .el-input__inner{
              border-radius: 45px;
            }
          }

        }
      }
    }
  }
  /deep/ .el-select .el-input__inner{
    border-radius: 45px;
  }

  ::v-deep .el-form-item__content{
    ::v-deep  .el-input__inner{
      border-radius: 45px;
    }
  }
  ::v-deep .el-form-item__label{
    color: #45524f;
  }
  .serverTable{
    position: absolute;
    width: 97%;
    padding: 0%;
    /*height: 100%;*/
    height: calc(100% - 140px);
  }
  /deep/ .cell{
    text-align: center;
  }
  /deep/ .el-table__empty-text{
    line-height:44px
  }
  /deep/ .el-table__empty-block{
    min-height: 44px;
  }
  /deep/ .el-table__body-wrapper {
    /*height: 200px; !* 滚动条整体高 必须项 *!*/
    border-right: none;
    overflow-y: scroll;/* overflow-y为了不出现水平滚动条*/
  }
  /deep/ .el-dialog--center .el-dialog__body{
    padding: 10px;
  }
  /deep/ .el-table__body-wrapper::-webkit-scrollbar {
    width:6px;/* 滚动条的宽高 必须项 */
    height: 5px;
  }

  /deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
    background-color: #bfcbd9;/* 滚动条的宽 */
    border-radius: 3px;
  }
  /deep/ .el-input__inner{
    border-radius: 45px;
  }
</style>
